


#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "headless.h"
#include "app_talise.h"
#include "axi_io.h"
#include "app_parameters.h"
#include "xscugic.h"

#include "talise_rx.h"


#include "talise_config.h"
#include "talise_agc.h"

int Init_Sys();
static void Freq_policy_Intr_Handler(void *callback);

static XScuGic Intc;
u8 mode_policy_flag;
u32 ModeFlag;

#define ju_report_power_data_LOW_ADDR			0x43C70300
#define ju_report_power_data_HIGH_ADDR			0x43C70304

#define ntr_report_freqdecision_data_ADDR		0x43C70200
#define ntr_receive_power_data_ADDR     		0x43C70000
#define ju_detect_power_data_ADDR     			0x43C70100


int main()
{
	u32 interf_point = 0x00010001;
    init_platform();
    print("Hello ADRV9009\n\r");

    Init_Sys();
	
	uint32_t ret = 0;
	uint8_t rxGainIndex = 0;
	uint16_t rxDecPower_mdBFS = 0;

	adrv9009_init();

	ret = TALISE_setupRxAgc(&tal,&rxAgcCtrl);
	if (ret != TALACT_NO_ACTION) {xil_printf("TALISE_setupRxAgc something wrong!");}

	ret = TALISE_setRxGainControlMode(&tal,TAL_AGCSLOW);
	if (ret != TALACT_NO_ACTION) {xil_printf("TALISE_setRxGainControlMode something wrong!");}

	axi_io_write(AXI_SIGNAL_0_BASEADDR, FH_select_vector_ADDR, 0);
	axi_io_write(AXI_SIGNAL_0_BASEADDR, DATA_CONSTANT_ADDR, 0x00007ff0);
	axi_io_write(AXI_SIGNAL_0_BASEADDR, DATA_SOURCE_ADDR, 0x1);

	//real monophonic
	axi_io_write(AXI_SIGNAL_0_BASEADDR, DATA_SOURCE_ADDR, 0x2);
	//cpomplex monophonic
	axi_io_write(AXI_SIGNAL_0_BASEADDR, DATA_SOURCE_ADDR, 0x3);

	axi_io_write(AXI_SIGNAL_0_BASEADDR, DATA_SOURCE_ADDR, 0x5);

	 while(1){
	 	sleep(1);

	 	ret = TALISE_getRxGain(&tal,TAL_RX1,&rxGainIndex);
	 	if (ret != TALACT_NO_ACTION) {xil_printf("TALISE_getRxGain something wrong!");}
	 	ret = TALISE_getRxDecPower(&tal,TAL_RX1,&rxDecPower_mdBFS);
	 	if (ret != TALACT_NO_ACTION) {xil_printf("TALISE_getRxDecPower something wrong!");}
	 	xil_printf("rxGainIndex: %d ,TALISE_getRxDecPower: %d;\n\r",rxGainIndex,rxDecPower_mdBFS);

		if(mode_policy_flag == 1) {
			if(ModeFlag == 0){ // ntr
				mode_policy_flag = 0;
				xil_printf("rec policy ntr intr\n\r");
				for (int i = 0; i < 24; i++)
				{
					uintptr_t target_address = ntr_receive_power_data_ADDR + i*8;
					uint32_t read_data_low = Xil_In32(target_address);
					target_address = target_address + 4;
					uint32_t read_data_high = Xil_In32(target_address);
					printf("terminal id : %d, data : %08X%08X\r\n", i, read_data_high,read_data_low);
				}

				Xil_Out32(ntr_report_freqdecision_data_ADDR,interf_point);
				interf_point = interf_point << 1;

			}else{ // ju
				mode_policy_flag = 0;
				uint32_t read_data[32] = {0};
				for (int j = 0; j < 2; j++)
				{
					for (int i = 0; i < 32; i++)
					{
						uintptr_t target_address = ju_detect_power_data_ADDR + i*4 + j*32*4;
						read_data[i] = read_data[i] + Xil_In32(target_address);
					}
				}

				for (int i = 0; i < 32; i++)
				{
					printf("freq num : %d, power data origin : %08X, int : %d\r\n", i, read_data[i], read_data[i]);
				}

				xil_printf("rec policy ju intr\n\r");
				Xil_Out32(ju_report_power_data_LOW_ADDR,0x12345678);
				Xil_Out32(ju_report_power_data_HIGH_ADDR,0x87654321);
			}
		}
	 };

	talise_shutdown(&tal);
    cleanup_platform();
    return 0;
}

int Init_Sys(){
	XScuGic * IntcInsPtr = &Intc;

	int Status;

	XScuGic_Config *IntcConfig;
	IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
	if (NULL == IntcConfig) {
		return XST_FAILURE;
	}

	Status = XScuGic_CfgInitialize(IntcInsPtr, IntcConfig,IntcConfig->CpuBaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	Xil_ExceptionInit();
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,(Xil_ExceptionHandler)XScuGic_InterruptHandler,(void *)IntcInsPtr);
	Xil_ExceptionEnable();

	Status = XScuGic_Connect(IntcInsPtr,64,(Xil_InterruptHandler) Freq_policy_Intr_Handler,NULL);

	XScuGic_Enable(IntcInsPtr,64);

	return XST_SUCCESS;


}

static void Freq_policy_Intr_Handler(void *callback)
{
	axi_io_read(AXI_SIGNAL_0_BASEADDR,28,&ModeFlag);
	if(mode_policy_flag == 0){
		mode_policy_flag = 1;
	}
}
